package com.snap.vseries.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.snap.vseries.AppConfig;
import com.snap.vseries.R;
import com.snap.vseries.bean.ExpEntity;
import com.snap.vseries.ui.TemplateActivity;
import com.snap.vseries.ui.TemplateNewActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 模板
 */
public class HomeTemplateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    final int[] ids = new int[]{R.drawable.pic_panel_g, R.drawable.pic_panel_b, R.drawable.pic_panel_c};
    private Context context;
    private List<ExpEntity> templateEntityList;
    private int TYPE_HEADER = 1001;

    public HomeTemplateAdapter(Context context, List<ExpEntity> templateEntityList) {
        this.context = context;
        this.templateEntityList = templateEntityList;
    }

    public void updateData(List<ExpEntity> dataList) {
        this.templateEntityList = dataList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View headerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_header_home_template, parent, false);
            return new HeaderViewHolder(headerView);
        }

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_template, parent, false);
        return new TemplateHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TemplateHolder) {
            ((TemplateHolder) holder).root.setBackgroundResource(ids[position % ids.length]);
            ((TemplateHolder) holder).textView.setText(templateEntityList.get(AppConfig.Dynamic.custom_setting ? (position - 1) : position).getTestProject());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TemplateActivity.class);
                    intent.putExtra("name", templateEntityList.get(AppConfig.Dynamic.custom_setting ? (position - 1) : position).getTestProject());
                    context.startActivity(intent);
                }
            });
        } else {
            ((HeaderViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TemplateNewActivity.class);
                    context.startActivity(intent);
                }
            });
        }
    }


    @Override
    public int getItemViewType(int position) {
        //在第一个位置添加头
        if (position == 0 && AppConfig.Dynamic.custom_setting) {
            return TYPE_HEADER;
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        if (templateEntityList == null || templateEntityList.size() == 0) {
            return AppConfig.Dynamic.custom_setting ? 1 : 0;
        }
        return AppConfig.Dynamic.custom_setting ? (templateEntityList.size() + 1) : templateEntityList.size();
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View headerView) {
            super(headerView);
        }
    }

    class TemplateHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.text_view)
        TextView textView;
        @BindView(R.id.root)
        FrameLayout root;

        TemplateHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
